import cacheFunc from '@/utils/cache.js'
export default {
	data() {
		return {
			mixinDistrict: '',
			weather: '',
			pcd: ''
		}
	},
	methods: {
		//   初次位置授权
		getAuthorize() {
			return new Promise((resolve, reject) => {
				uni.authorize({
					scope: "scope.userLocation",
					success: () => {
						resolve(); // 允许授权
					},
					fail: () => {
						reject(); // 拒绝授权
					},
				});
			});
		},
		getWeather(city) {
			uni.request({
				url: 'http://wthrcdn.etouch.cn/weather_mini?city=' + city,
				method: 'GET',
				success: res => {

					if (res.data.data) {
						let wendu = Math.round(res.data.data.wendu)

						const {
							forecast
						} = res.data.data
						this.weather = `${forecast[0].type} ${wendu}℃`
					}

				},
				fail: () => {
					this.openmsg("警告", "天气接口获取失败")
				},
				complete: () => {}
			});
		},
		// 确认授权后，获取用户位置
		getLocationInfo() {
			return new Promise((resolve, reject) => {
				const that = this;
				uni.getLocation({
					type: "gcj02",
					geocode: true, //获取当前城市信息
					success: function(res) {
						console.log(res, '地理位置')
						const {
							address
						} = res
						if (address) {
							// 
							const {
								province,
								city,
								district
							} = address
							// cacheFunc.setCache('locaAddress', JSON.stringify(address))
							that.mixinDistrict = address.district
							getApp().globalData.district = address.district;
							that.pcd = province + city + district;
							// 获取温度
							that.getWeather(address.district)
							console.log(that.pcd, 'getApp().globalData.district')
							resolve({
								...res,
								pcd: province + city + district
							})
						}
						that.longitude = res.longitude; //118.787575;
						that.latitude = res.latitude; //32.05024;

						console.log("获取当前的用户经度", that.longitude);
						console.log("获取当前的用户纬度", that.latitude);
						var long = 0;
						var lat = 0;
						//小数点保留六位  经度
						if (that.longitude.toString().indexOf('.') > 0) {
							const longlatsplit = that.longitude.toString().split('.');
							if (longlatsplit.length >= 2) {
								long = parseFloat(longlatsplit[0] === "" ? 0 : longlatsplit[0]) +
									parseFloat(
										"." +
										longlatsplit[1].slice(0, 6));
							}
						}
						if (that.latitude.toString().indexOf('.') > 0) {
							const longlatsplit1 = that.latitude.toString().split('.');
							if (longlatsplit1.length >= 2) {
								lat = parseFloat(longlatsplit1[0] === "" ? 0 : longlatsplit1[0]) +
									parseFloat(
										"." +
										longlatsplit1[1].slice(0, 6));
							}
						}

						// cookie.set("longitude", long);
						// cookie.set("latitude", lat);
						cacheFunc.setCache('longitude', long)
						cacheFunc.setCache('latitude', lat)
						console.log("纬度", lat);
						// this.distance(that.latitude,that.longitude);
					},
					fail(err) {
						console.log("err", err);
						reject(err)
					}
				});
			})

		},
		// 拒绝授权后，弹框提示是否手动打开位置授权
		openConfirm() {
			return new Promise((resolve, reject) => {
				uni.showModal({
					title: "请求授权当前位置",
					content: "我们需要获取地理位置信息，为您推荐附近的美食",
					success: (res) => {
						if (res.confirm) {
							uni.openSetting({
								success(res) {
									console.log(res, '========')
								}
							})
							// uni.openSetting({
							// 	success(res) {
							// 		if (res[1].authSetting["scope.userLocation"] === true) {
							// 			resolve(); // 打开地图权限设置
							// 		} else {
							// 			reject();
							// 		}
							// 	}
							// })


						} else if (res.cancel) {
							reject();
						}
					},
				});
			});
		},
		// 彻底拒绝位置获取
		rejectGetLocation() {
			uni.showToast({
				title: "你拒绝了授权，无法获得周边信息",
				icon: "none",
				duration: 2000,
			});
		},
		getList() {
			console.log("获取周围美食");
		},
		onReady() {
			this.getLocationInfo();
		}
	}

}
